#include "propertytree.h"
PropertyTree::PropertyTree(){}
void PropertyTree::loadXML2List(ExpenseRecordList& LIST, IncomeRecordList& LIST2, AlertList &LIST3, string& accountname)
{
    //clear pt
    pt.clear();
    //clear list
    LIST.clearList();
    LIST.clearCategoryList();
    LIST2.clearList();
    LIST2.clearCategoryList();
    //load xml data to tree
    ifstream XMLFile;

    XMLFile.open("ExpenseRecord.XML");

    if(XMLFile.good()){
        stringstream data;
        data<<XMLFile.rdbuf();

        read_xml(data,pt,trim_whitespace);

        //load tree to list

        BOOST_FOREACH(ptree::value_type const& val,pt.get_child("ExpenseRecordList"))
        {
            if(val.first=="ExpenseRecord")
            {
                Record r;//ID number, Amount, Date, Category, Description, Type
                r.setid(val.second.get<string>("<xmlattr>.id"));
                r.setname(val.second.get<string>("name"));
                r.setnameid(val.second.get<string>("nameID"));
                r.setamount(val.second.get<double>("amount"));
                r.setdatetime(val.second.get<string>("date"));
                r.setcategory(val.second.get<string>("category"));
                r.setinfo(val.second.get<string>("description"));
                LIST.addRecord(r);

            }
        }

        BOOST_FOREACH(ptree::value_type const& val, pt.get_child("IncomeRecordList"))
        {
            if(val.first=="IncomeRecord")
            {
                Record r;//ID number, Amount, Date, Category, Description, Type
                r.setid(val.second.get<string>("<xmlattr>.id"));
                r.setname(val.second.get<string>("name"));
                r.setnameid(val.second.get<string>("nameID"));
                r.setamount(val.second.get<double>("amount"));
                r.setdatetime(val.second.get<string>("date"));
                r.setcategory(val.second.get<string>("category"));
                r.setinfo(val.second.get<string>("description"));
                LIST2.addRecord(r);
            }
        }

        BOOST_FOREACH(ptree::value_type const& val, pt.get_child("AlertList"))
        {
            if(val.first=="Alert")
            {
                Alert r;//ID number, Amount, Date, Category, Description, Type
                r.setid(val.second.get<string>("<xmlattr>.id"));
                r.setname(val.second.get<string>("name"));
                r.setnameid(val.second.get<string>("nameID"));
                r.setdatetime(val.second.get<string>("date"));
                r.setinfo(val.second.get<string>("description"));
                r.setrecurring(val.second.get<bool>("recurring"));
                r.setfrequency(val.second.get<string>("frequency"));
                r.setactivated(val.second.get<bool>("activated"));
                r.setdisabled(val.second.get<bool>("disabled"));
                LIST3.addAlert(r);
            }
        }

        BOOST_FOREACH(ptree::value_type const& val,pt.get_child("ExpenseCategoryList"))
        {
            if(val.first == "ExpenseCategory")
                LIST.addCategory(QString::fromStdString(val.second.get<string>("<xmlattr>.id")));
        }

        BOOST_FOREACH(ptree::value_type const& val,pt.get_child("IncomeCategoryList"))
        {
            if(val.first == "IncomeCategory")
                LIST2.addCategory(QString::fromStdString(val.second.get<string>("<xmlattr>.id")));
        }

        BOOST_FOREACH(ptree::value_type const& val,pt.get_child("AccountName"))
        {
            if(val.first == "Name")
                accountname = val.second.get<string>("<xmlattr>.id");
        }
        //clear pt

        pt.clear();
    }
    XMLFile.close();
}
void PropertyTree::saveList2XML(ExpenseRecordList& LIST, IncomeRecordList& LIST2, AlertList& LIST3, string& accountname)
{
    //clear pt
    pt.clear();

    QDateTime current = QDateTime::currentDateTime();
    QDateTime epoch = QDateTime(QDate(1970,1,1), QTime(1,1,1));

    ptree allRecords;
    string buffer;
    version = QString::number(epoch.secsTo(current)).toStdString();
    pt.add("version",version);

    BOOST_FOREACH(Record r,LIST.getRecordList())
    {
        buffer = r.sgetdatetime().toStdString();
        ptree recordNode;//ID number, Amount, Date, Info
        recordNode.add("<xmlattr>.id",r.getid());
        recordNode.add("name",r.getname());
        recordNode.add("nameID", r.getnameid());
        recordNode.add("amount",r.getamount());
        recordNode.add("date",buffer);
        recordNode.add("category",r.getcategory());
        recordNode.add("description",r.getinfo());
        allRecords.add_child("ExpenseRecord",recordNode);
    }

    pt.add_child("ExpenseRecordList",allRecords);

    allRecords.clear();

    BOOST_FOREACH(Record r,LIST2.getRecordList())
    {
        ptree recordNode;//ID number, Amount, Date, Info
        recordNode.add("<xmlattr>.id",r.getid());
        recordNode.add("name",r.getname());
        recordNode.add("nameID", r.getnameid());
        recordNode.add("amount",r.getamount());
        recordNode.add("date",r.sgetdatetime().toStdString());
        recordNode.add("category",r.getcategory());
        recordNode.add("description",r.getinfo());
        allRecords.add_child("IncomeRecord",recordNode);
    }
    pt.add_child("IncomeRecordList",allRecords);

    allRecords.clear();

    BOOST_FOREACH(Alert r,LIST3.getAlertList())
    {
        ptree recordNode;//ID number, Amount, Date, Info
        recordNode.add("<xmlattr>.id",r.getid());
        recordNode.add("name",r.getname());
        recordNode.add("nameID", r.getnameid());
        recordNode.add("date",r.sgetdatetime().toStdString());
        recordNode.add("description",r.getinfo());
        recordNode.add("recurring", r.getrecurring());
        recordNode.add("frequency", r.getfrequency());
        recordNode.add("activated", r.getactivated());
        recordNode.add("disabled", r.getdisabled());
        allRecords.add_child("Alert",recordNode);
    }

    pt.add_child("AlertList",allRecords);

    allRecords.clear();

    BOOST_FOREACH(QString r,LIST.getCategoryList())
    {
        ptree recordNode;
        string input = r.toStdString();
        recordNode.add("<xmlattr>.id",input);
        allRecords.add_child("ExpenseCategory", recordNode);
    }
    pt.add_child("ExpenseCategoryList",allRecords);
    allRecords.clear();

    BOOST_FOREACH(QString r,LIST2.getCategoryList())
    {
        ptree recordNode;
        string input = r.toStdString();
        recordNode.add("<xmlattr>.id",input);
        allRecords.add_child("IncomeCategory", recordNode);
    }
    pt.add_child("IncomeCategoryList",allRecords);
    allRecords.clear();

    ptree recordNode;

    recordNode.add("<xmlattr>.id", accountname);
    allRecords.add_child("Name", recordNode);
    pt.add_child("AccountName",allRecords);
    allRecords.clear();

    recordNode.clear();

    //remove old XML file
    remove("ExpenseRecord.XML");

    //write pt to new XML file
    write_xml("ExpenseRecord.XML",pt);

    //clear pt
    pt.clear();
}

void PropertyTree::print(void)
{
    qDebug()<<"SUCCEEDED"<<endl;
    qDebug()<<"current file version: "<<this->version_num<<endl;
}
int PropertyTree::version_num = 0;
